home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / lisp / wgdb-42.lha / wgdb-4.2 / bfd / cache.c < prev    next >
C/C++ Source or Header  |  1992-09-11  |  7KB  |  263 lines

  1. /* BFD library -- caching of file descriptors.
  2.    Copyright (C) 1990-1991 Free Software Foundation, Inc.
  3.    Hacked by Steve Chamberlain of Cygnus Support (steve@cygnus.com).
  4.  
  5. This file is part of BFD, the Binary File Descriptor library.
  6.  
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11.  
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with this program; if not, write to the Free Software
  19. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  20.  
  21. /*doc*
  22. @section File Caching
  23. The file caching mechanism is embedded within BFD and allows the application to open as many
  24. BFDs as it wants without regard to the underlying operating system's
  25. file descriptor limit (often as low as 20 open files).
  26.  
  27. The module in @code{cache.c} maintains a least recently used list of
  28. @code{BFD_CACHE_MAX_OPEN} files, and exports the name
  29. @code{bfd_cache_lookup} which runs around and makes sure that the
  30. required BFD is open. If not, then it chooses a file to close, closes
  31. it and opens the one wanted, returning its file handle.
  32.  
  33. */
  34.  
  35. /* $Id: cache.c,v 1.11 1991/10/11 10:05:24 gnu Exp $ */
  36.  
  37. #include "bfd.h"
  38. #include "sysdep.h"
  39. #include "libbfd.h"
  40.  
  41. /*proto-internal* BFD_CACHE_MAX_OPEN
  42. The maxiumum number of files which the cache will keep open at one
  43. time.
  44. *+
  45. #define BFD_CACHE_MAX_OPEN 10
  46. *-
  47.  
  48. */
  49.  
  50.  
  51. static int open_files;
  52.  
  53. static bfd *cache_sentinel;    /* Chain of BFDs with active fds we've
  54.                    opened */
  55.  
  56. /*proto-internal*  bfd_last_cache
  57. Zero, or a pointer to the topmost BFD on the chain.  This is used by
  58. the @code{bfd_cache_lookup} macro in @file{libbfd.h} to determine when
  59. it can avoid a function call.
  60. *+
  61. extern bfd *bfd_last_cache;
  62. *-
  63.  
  64. */
  65.  
  66. bfd *bfd_last_cache;
  67.  
  68. /*proto-internal*  bfd_cache_lookup
  69. Checks to see if the required BFD is the same as the last one looked
  70. up. If so then it can use the iostream in the BFD with impunity, since
  71. it can't have changed since the last lookup, otherwise it has to
  72. perform the complicated lookup function
  73. *+
  74. #define bfd_cache_lookup(x) \
  75.      ((x)==bfd_last_cache? \
  76.         (FILE*)(bfd_last_cache->iostream): \
  77.          bfd_cache_lookup_worker(x))
  78.  
  79. *-
  80.  
  81. */
  82.  
  83. static void bfd_cache_delete();
  84.  
  85.  
  86. static void
  87. DEFUN_VOID(close_one)
  88. {
  89.     bfd *kill = cache_sentinel;
  90.     if (kill == 0)        /* Nothing in the cache */
  91.     return ;
  92.  
  93.     /* We can only close files that want to play this game.  */
  94.     while (!kill->cacheable) {
  95.     kill = kill->lru_prev;
  96.     if (kill == cache_sentinel) /* Nobody wants to play */
  97.        return ;
  98.     }
  99.  
  100.     kill->where = ftell((FILE *)(kill->iostream));
  101.     bfd_cache_delete(kill);
  102. }
  103.  
  104. /* Cuts the BFD abfd out of the chain in the cache */
  105. static void 
  106. DEFUN(snip,(abfd),
  107.       bfd *abfd)
  108. {
  109.   abfd->lru_prev->lru_next = abfd->lru_next;
  110.   abfd->lru_next->lru_prev = abfd->lru_prev; 
  111.   if (cache_sentinel == abfd) cache_sentinel = (bfd *)NULL;
  112. }
  113.  
  114. static void
  115. DEFUN(bfd_cache_delete,(abfd),
  116.       bfd *abfd)
  117. {
  118.   fclose ((FILE *)(abfd->iostream));
  119.   snip (abfd);
  120.   abfd->iostream = NULL;
  121.   open_files--;
  122.   bfd_last_cache = 0;
  123. }
  124.   
  125. static bfd *
  126. DEFUN(insert,(x,y),
  127.       bfd *x AND
  128.       bfd *y)
  129. {
  130.   if (y) {
  131.     x->lru_next = y;
  132.     x->lru_prev = y->lru_prev;
  133.     y->lru_prev->lru_next = x;
  134.     y->lru_prev = x;
  135.  
  136.   }
  137.   else {
  138.     x->lru_prev = x;
  139.     x->lru_next = x;
  140.   }
  141.   return x;
  142. }
  143.  
  144.  
  145. /*proto-internal*
  146. *i bfd_cache_init
  147. Initialize a BFD by putting it on the cache LRU.
  148. *; PROTO(void, bfd_cache_init, (bfd *));
  149. *-*/
  150.  
  151. void
  152. DEFUN(bfd_cache_init,(abfd),
  153.       bfd *abfd)
  154. {
  155.   cache_sentinel = insert(abfd, cache_sentinel);
  156. }
  157.  
  158.  
  159. /*proto-internal*
  160. *i bfd_cache_close
  161. Remove the BFD from the cache. If the attached file is open, then close it too.
  162. *; PROTO(void, bfd_cache_close, (bfd *));
  163. *-*/
  164. void
  165. DEFUN(bfd_cache_close,(abfd),
  166.       bfd *abfd)
  167. {
  168.   /* If this file is open then remove from the chain */
  169.   if (abfd->iostream) 
  170.     {
  171.       bfd_cache_delete(abfd);
  172.     }
  173. }
  174.  
  175. /*proto-internal*
  176. *i bfd_open_file
  177. Call the OS to open a file for this BFD.  Returns the FILE *
  178. (possibly null) that results from this operation.  Sets up the
  179. BFD so that future accesses know the file is open. If the FILE *
  180. returned is null, then there is won't have been put in the cache, so
  181. it won't have to be removed from it.
  182. *; PROTO(FILE *, bfd_open_file, (bfd *));
  183. *-*/
  184. FILE *
  185. DEFUN(bfd_open_file, (abfd),
  186.       bfd *abfd)
  187. {
  188.   abfd->cacheable = true;    /* Allow it to be closed later. */
  189.   if(open_files >= BFD_CACHE_MAX_OPEN) {
  190.     close_one();
  191.   }
  192.   switch (abfd->direction) {
  193.   case read_direction:
  194.   case no_direction:
  195.     abfd->iostream = (char *) fopen(abfd->filename, "r");
  196.     break;
  197.   case both_direction:
  198.   case write_direction:
  199.     if (abfd->opened_once == true) {
  200.       abfd->iostream = (char *) fopen(abfd->filename, "r+");
  201.       if (!abfd->iostream) {
  202.     abfd->iostream = (char *) fopen(abfd->filename, "w+");
  203.       }
  204.     } else {
  205.       /*open for creat */
  206.       abfd->iostream = (char *) fopen(abfd->filename, "w");
  207.       abfd->opened_once = true;
  208.     }
  209.     break;
  210.   }
  211.   if (abfd->iostream) {
  212.     open_files++;
  213.     bfd_cache_init (abfd);
  214.   }
  215.  
  216.   return (FILE *)(abfd->iostream);
  217. }
  218.  
  219. /*proto-internal*
  220. *i bfd_cache_lookup_worker
  221. Called when the macro @code{bfd_cache_lookup} fails to find a quick
  222. answer. Finds a file descriptor for this BFD.  If necessary, it open it.
  223. If there are already more than BFD_CACHE_MAX_OPEN files open, it trys to close
  224. one first, to avoid running out of file descriptors. 
  225. *; PROTO(FILE *, bfd_cache_lookup_worker, (bfd *));
  226.  
  227. *-*/
  228.  
  229. FILE *
  230. DEFUN(bfd_cache_lookup_worker,(abfd),
  231.       bfd *abfd)
  232. {
  233.   if (abfd->my_archive) 
  234.       {
  235.     abfd = abfd->my_archive;
  236.       }
  237.   /* Is this file already open .. if so then quick exit */
  238.   if (abfd->iostream) 
  239.       {
  240.     if (abfd != cache_sentinel) {
  241.       /* Place onto head of lru chain */
  242.       snip (abfd);
  243.       cache_sentinel = insert(abfd, cache_sentinel);
  244.     }
  245.       }
  246.   /* This is a BFD without a stream -
  247.      so it must have been closed or never opened.
  248.      find an empty cache entry and use it.  */
  249.   else 
  250.       {
  251.  
  252.     if (open_files >= BFD_CACHE_MAX_OPEN) 
  253.         {
  254.           close_one();
  255.         }
  256.  
  257.     BFD_ASSERT(bfd_open_file (abfd) != (FILE *)NULL) ;
  258.     fseek((FILE *)(abfd->iostream), abfd->where, false);
  259.       }
  260.   bfd_last_cache = abfd;
  261.   return (FILE *)(abfd->iostream);
  262. }
  263.